//
//  RainbowView.m
//  霓虹灯
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 zuzhiming. All rights reserved.
//
#define MAS_SHORTHAND // 1.对于约束参数可以省去 "mas_"
#define MAS_SHORTHAND_GLOBALS // 2.对于默认的约束参数自动装箱
#import "RainbowView.h"
#import "Masonry.h"
@interface RainbowView ()
@property (nonatomic, retain) UIView *redView;
@property (nonatomic, retain) UIView *orangeView;
@property (nonatomic, retain) UIView *yellowView;
@property (nonatomic, retain) UIView *greenView;
@property (nonatomic, retain) UIView *cyanView;
@property (nonatomic, retain) UIView *blueView;
@property (nonatomic, retain) UIView *purpleView;
@property (nonatomic, assign) CGFloat timeInterval;


@end

@implementation RainbowView

- (void)dealloc
{
    [_redView release];
    [_orangeView release];
    [_yellowView release];
    [_greenView release];
    [_cyanView release];
    [_blueView release];
    [_purpleView release];
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame TimeInterval:(CGFloat)time
{
    self = [super initWithFrame:frame];
    if (self) {
        //加载子视图
        [self createSubViews];
        //时间间隔由外界传入
        self.timeInterval = time;
        //开启定时器
        [self addTimer];
    }
    return self;
}

//添加定时器
- (void)addTimer
{
    [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(SubViewsBackGroundColorBeginTransform) userInfo:nil repeats:YES];
    
}

//初始化彩色图层
- (void)createSubViews
{
    self.redView = [[UIView alloc] init];
    self.redView.frame = self.bounds;
    self.redView.backgroundColor = [UIColor redColor];
    [self addSubview:_redView];
    [_redView release];
    
    self.orangeView = [[UIView alloc] init];
    self.orangeView.backgroundColor = [UIColor orangeColor];
    [self.redView addSubview:_orangeView];
    [_orangeView release];
    
    self.yellowView = [[UIView alloc] init];
    self.yellowView.backgroundColor = [UIColor yellowColor];
    [self.orangeView addSubview:_yellowView];
    [_yellowView release];
    
    self.greenView = [[UIView alloc] init];
    self.greenView.backgroundColor = [UIColor greenColor];
    [self.yellowView addSubview:_greenView];
    [_greenView release];
    
    self.cyanView = [[UIView alloc] init];
    self.cyanView.backgroundColor = [UIColor cyanColor];
    [self.greenView addSubview:_cyanView];
    [_cyanView release];
    
    self.blueView = [[UIView alloc] init];
    self.blueView.backgroundColor = [UIColor blueColor];
    [self.cyanView addSubview:_blueView];
    [_blueView release];
    
    self.purpleView = [[UIView alloc] init];
    self.purpleView.backgroundColor = [UIColor purpleColor];
    [self.blueView addSubview:_purpleView];
    [_purpleView release];
    
}

//布局约束
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.orangeView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    [self.yellowView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    [self.greenView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    [self.cyanView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    [self.blueView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
    
    [self.purpleView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(UIEdgeInsetsMake(20, 20, 20, 20));
    }];
}

//改变背景色
- (void)SubViewsBackGroundColorBeginTransform
{
    //通过中间变量 实现每个背景色之间的交换
    UIView *midView = [[UIView alloc] init];
    
    [UIView animateWithDuration:self.timeInterval animations:^{
        
        midView.backgroundColor = self.purpleView.backgroundColor;
        self.purpleView.backgroundColor = self.redView.backgroundColor;
        self.redView.backgroundColor = self.orangeView.backgroundColor;
        self.orangeView.backgroundColor = self.yellowView.backgroundColor;
        self.yellowView.backgroundColor = self.greenView.backgroundColor;
        self.greenView.backgroundColor = self.cyanView.backgroundColor;
        self.cyanView.backgroundColor = self.blueView.backgroundColor;
        self.blueView.backgroundColor = midView.backgroundColor;
        [midView release];
        
        
    }];
    
}


@end



